package enterprise.manage.web.controller.order;

import enterprise.manage.dao.OrderDao;
import enterprise.manage.entity.Orders;
import freemarker.template.utility.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-11-25
 * Desc:
 */
public class OrderHandlerController extends AbstractController {
    @Autowired
    OrderDao orderDao;
    public static final String DEFAULT_PAGE = "order/view";

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String method = request.getParameter("method");
        String id = request.getParameter("id").trim();
        Object entity = getEntity(id);
        String pageName;

        if ("edit".equals(method)) {
            pageName = editPage(request);
        } else if ("add".equals(method)) {
            pageName = add(request);
        } else if ("outStock".equals(method)){
            pageName = outStockPage(request);
        } else {
            pageName = view();
        }
        request.setAttribute("method", method);
        request.setAttribute("entity", entity);
        return new ModelAndView(pageName);
    }

    public Object getEntity(String id) {
        if (StringUtils.isNotBlank(id)) {
            return orderDao.findByUnique("id", Long.parseLong(id));
        } else {
            return new Orders();
        }
    }

    /**
     * 初始化 编辑页面
     *
     * @param request
     * @return
     */
    public String editPage(HttpServletRequest request) {
        return "order/edit";
    }
     public String outStockPage(HttpServletRequest request) {
        return "order/outStock";
    }
    /**
     * 初始化添加页面
     *
     * @param request
     * @return
     */
    public String add(HttpServletRequest request) {
        return "order/edit";
    }

    /**
     * 初始化详情页面
     *
     * @return
     */
    public String view() {
        return "order/view";
    }


    public Object getEntity(Long id) {
        return orderDao.get(id);
    }


}
